package com.wx.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.wx.kit.WxKit;
import com.wx.model.wxDb.BaseParameter;
import com.wx.model.wxDb.WxCustomer;
import com.wx.model.wxDb.WxGroup;
import com.wx.repository.wxRepository.BaseParameterRepository;

import net.minidev.json.JSONValue;

@RestController
@RequestMapping(path = "/customerManager")
public class CustomerManagerController {
	@Resource
	private BaseParameterRepository baseParameterRepository;

	@SuppressWarnings("unchecked")
	@GetMapping(path = "/getCustomerArr")
	public Map<String, Object> getCutomerArr() {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			String accessToken = "";
			List<BaseParameter> baseParameterArr = (List<BaseParameter>) this.baseParameterRepository.findAll();
			accessToken = baseParameterArr.get(0).getAccessToken();
			String customerArrMsg = WxKit.getUserList(accessToken);
			Map<String, Object> openIdMap = (Map<String, Object>) JSONValue.parse(customerArrMsg);
			String data = openIdMap.get("data").toString();
			Map<String, Object> dataMap = (Map<String, Object>) JSONValue.parse(data);
			String openidStr = dataMap.get("openid").toString();
			List<String> openIdArr = (List<String>) JSONValue.parse(openidStr);
			List<WxCustomer> wxCustomerArr = new ArrayList<WxCustomer>();
			for (int i = 0; i < openIdArr.size(); i++) {
				String customerDate = WxKit.getUserDateByOpenId(accessToken, openIdArr.get(i));
				Map<String, Object> customerDateMap = (Map<String, Object>) JSONValue.parse(customerDate);
				WxCustomer wxCustomer = new WxCustomer();
				wxCustomer.setSubscribe((Integer.parseInt(customerDateMap.get("subscribe").toString())));
				wxCustomer.setOpenid(customerDateMap.get("openid").toString());
				wxCustomer.setNickname(customerDateMap.get("nickname").toString());
				wxCustomer.setSex(Integer.parseInt(customerDateMap.get("sex").toString()));
				wxCustomer.setLanguage(customerDateMap.get("language").toString());
				wxCustomer.setCity(customerDateMap.get("city").toString());
				wxCustomer.setProvince(customerDateMap.get("province").toString());
				wxCustomer.setCountry(customerDateMap.get("country").toString());
				wxCustomer.setHeadimgurl(customerDateMap.get("headimgurl").toString());
				wxCustomer.setSubscribeTime(Long.parseLong(customerDateMap.get("subscribe_time").toString()));
				wxCustomer.setRemark(customerDateMap.get("remark").toString());
				wxCustomer.setGroupid(Integer.parseInt(customerDateMap.get("groupid").toString()));
				wxCustomer.setTagidList((List<Integer>) customerDateMap.get("tagid_list"));
				wxCustomer.setSubscribeScene(customerDateMap.get("subscribe_scene").toString());
				wxCustomer.setQrScene(Integer.parseInt(customerDateMap.get("qr_scene").toString()));
				wxCustomer.setQrSceneStr(customerDateMap.get("qr_scene_str").toString());
				wxCustomerArr.add(wxCustomer);
			}
			resultMap.put("arr", wxCustomerArr);
			resultMap.put("success", true);
			System.err.println("获取用户信息成功");
		} catch (Exception e) {
			e.printStackTrace();
		}

		return resultMap;
	}

	@GetMapping(path = "/getCustomerGroup")
	public Map<String, Object> getCustomerGroup() {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		String accessToken = "";
		List<BaseParameter> baseParameterArr = (List<BaseParameter>) this.baseParameterRepository.findAll();
		accessToken = baseParameterArr.get(0).getAccessToken();
		String groupMsg = WxKit.getUserGroups(accessToken);
		Map<String, List<Map<String, Object>>> groupJosnMap = (Map<String, List<Map<String, Object>>>) JSONValue.parse(groupMsg);
		List<Map<String, Object>> groupsArr = groupJosnMap.get("groups");
		System.out.println(groupsArr.get(0));
		List<WxGroup> wxGroupList = new ArrayList<WxGroup>();
		for (int i = 0; i < groupsArr.size(); i++) {
			WxGroup wxGroup = new WxGroup();
			wxGroup.setId(Integer.parseInt(groupsArr.get(i).get("id").toString()));
			wxGroup.setCount(Integer.parseInt(groupsArr.get(i).get("count").toString()));
			wxGroup.setName(groupsArr.get(i).get("name").toString());
			wxGroupList.add(wxGroup);
		}
		resultMap.put("wxGroupList", wxGroupList);
		return resultMap;
	}
}
